const bannerSchema = require('./schema/bannerSchema')

const bannerModel = {}

bannerModel.addBanner = (banner) => {
  const data = new bannerSchema({
    name: banner.name,
    desc: banner.desc,
    imgurl: banner.imgurl,
    imgbase: banner.imgbase,
    status: banner.status
  })
  return new Promise((resolve, reject) => {
    data.save((err, res) => {
      if(err) {
        reject(err)
      } else {
        resolve(res)
      }
    })
  })
}

bannerModel.getAllBanner = () => {
  return new Promise((resolve, reject) => {
    bannerSchema.find().exec((err, rows) => {
      if (err) {
        reject(err)
      } else {
        resolve(rows)
      }
    })
  })
}

bannerModel.updateBannerStatus = (id, status) => {
  return new Promise((resolve, reject) => {
    bannerSchema.findOneAndUpdate({_id: id}, {$set: {status: status}}, (err, rows) => {
      if (err) {
        reject(err)
      } else {
        resolve(rows)
      }
    })
  })
}

bannerModel.getIndexBanner = () => {
  return new Promise((resolve, reject) => {
    bannerSchema.find({status: 1}).exec((err, rows) => {
      if (err) {
        reject(err)
      } else {
        resolve(rows)
      }
    })
  })
}

module.exports = bannerModel
